#include<stdio.h>
#include<string.h>                                                              
#include<unistd.h>
#include<pthread.h>
#include<fcntl.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<sys/poll.h>

#define SERVERPORT 8000
#define BUFSIZE 1024
#define FDSSIZE 2

int createsock(int port)
{
    int lfd=socket(AF_INET,SOCK_STREAM,0);
    if(lfd==-1)
    {
        perror("socket error");
        exit(1);
    }
    struct sockaddr_in serveraddr;
    serveraddr.sin_family=AF_INET;
    serveraddr.sin_port=htons(SERVERPORT);
    serveraddr.sin_addr.s_addr=0;//任意ip
    //绑定ip和端口号
    bind(lfd,(struct sockaddr*)&serveraddr,sizeof(serveraddr));
    listen(lfd,64);
    return lfd;
}

int main(int argc, char* argv[])
{
    int lfd=createsock(SERVERPORT);
    int poll_ret;
    char buf[BUFSIZE];

    struct pollfd fds[FDSSIZE];
    for(int i=0;i<FDSSIZE;i++)
    {
        fds[i].fd=-1;
        fds[i].events=0;
    }//初始化结构体数组
    fds[0].fd=lfd;
    fds[0].events=POLLIN;
    int nfds=1;

    while(1)
    {
        poll_ret=poll(fds,nfds,-1);
        if(poll_ret==-1)
        {
            perror("poll error");
            exit(1);
        }
        if(poll_ret==0)
        {
            sleep(1);
        }
        if(fds[0].revents & POLLIN)
        {
            int i;
            for(i=0;i<FDSSIZE;i++)
            {
                if(fds[i].fd==-1)
                {
                    break;
                }
            }
            if(i==FDSSIZE)
            {
                printf("too many client,请稍后重试\n");
                exit(1);
            }
            else
            {
                int cfd=accept(fds[0].fd,NULL,NULL);
                printf("客户端建立连接\n");
                if(cfd==-1)
                {
                    perror("accept error");
                    exit(1);
                }
                
                fds[i].fd=cfd;
                fds[i].events=POLLIN;
                printf("cfd成功放入数组中\n");
                if(i>nfds-1)
                {
                    nfds=i+1;
                }
                if(--poll_ret==0)
                {
                    continue;
                }
            }
        }
        for(int i=1;i<nfds;i++)
        {
            if(fds[i].revents & POLLIN)
            {
                int read_ret=read(fds[i].fd,buf,BUFSIZE);
                if(read_ret==-1)
                {
                    perror("error read");
                    exit(1);
                }
                else if(read_ret==0)
                {
                    printf("有客户端退出\n");
                    close(fds[i].fd);
                    fds[i].fd=-1;
                    fds[i].events=0;
                }
                else
                {
                    write(fds[i].fd,buf,read_ret);
                    printf("*******************\n");
                    write(STDOUT_FILENO,buf,read_ret);
                    printf("*******************\n");
                }
                if(--poll_ret==0)
                {
                    break;
                }
            }
        }
    }
    return 0;
}


